Due to the way IPython runs system commands by going out to a shell and then
coming back the system cd
won't work.
Fortunately IPython gives us the %cd magic command tht does work. It also
keeps a history of directories visited and has an integrated bookmark
system. It will also tab complete nicely, indeed just typing cd <tab>
will
give you a list of directories below your current location.
If you've downloaded this IPython notebook with the entire git repository then you can work along with me. I have a nice example directory tree for us to use.
In the examples below you can see they all start with /Users/tonyw/dev/MacSys
- the
first three parts are the directory where I keep all my github repositories and MacSys
is the local name for this git repository. You should probably select Cell - All Output -
Clear and remove all my output and ready for you to work through the examples
In [ ]:
pwd
In [ ]:
%cd example
In [ ]:
%bookmark ex
In [ ]:
%cd dir_one
In [ ]:
%bookmark one
In [ ]:
%cd ../dir_two
In [ ]:
%bookmark two
In [ ]:
%cd below/further_below
In [ ]:
%bookmark below
In [ ]:
%dhist
The directory history is kept in the list _dh
In [ ]:
_dh
In [ ]:
bookmark -l
In [ ]:
cd -b ex
If there is no directory with the same name we don't need the -b
In [ ]:
cd below
cd -
takes us to the previous directory
In [ ]:
cd -
cd -<n>
will go to entry <n>
in the history list.
In [ ]:
cd -4
cd --<text>
will go to a directory that matches <text>
in the history.
In [ ]:
cd --one
You will discover that bookmarks are saved from one IPython session to the next, but not the directory history.
I spent some time developing a way around this but discovered that a long directory history is less useful than a current one. I tend to have a longish bookmark list however.
IPython also has a directory stack. We can push a directory onto the stack, pop the top one off or list the current stack.
In [ ]:
cd below
In [ ]:
pushd ex
In [ ]:
pushd two
In [ ]:
dirs
In [ ]:
cd one
In [ ]:
pwd
In [ ]:
popd
In [ ]:
pwd
In [ ]:
popd
In [ ]:
dirs
In [ ]: